package org.zky.modules.merchant.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zky.aop.log.Log;
import org.zky.modules.merchant.domain.ZkyCourseTab;
import org.zky.modules.merchant.service.ZkyCourseTabService;
import org.zky.modules.merchant.service.dto.ZkyCourseTabQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author chenglong
* @date 2020-03-23
*/
@Api(tags = "course管理")
@RestController
@RequestMapping("/api/zkyCourseTab")
public class ZkyCourseTabController {

    private final ZkyCourseTabService zkyCourseTabService;

    public ZkyCourseTabController(ZkyCourseTabService zkyCourseTabService) {
        this.zkyCourseTabService = zkyCourseTabService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zkyCourseTab:list')")
    public void download(HttpServletResponse response, ZkyCourseTabQueryCriteria criteria) throws IOException {
        zkyCourseTabService.download(zkyCourseTabService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询course")
    @ApiOperation("查询course")
    @PreAuthorize("@el.check('zkyCourseTab:list')")
    public ResponseEntity<Object> getZkyCourseTabs(ZkyCourseTabQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(zkyCourseTabService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增course")
    @ApiOperation("新增course")
    @PreAuthorize("@el.check('zkyCourseTab:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ZkyCourseTab resources){
        return new ResponseEntity<>(zkyCourseTabService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改course")
    @ApiOperation("修改course")
    @PreAuthorize("@el.check('zkyCourseTab:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ZkyCourseTab resources){
        zkyCourseTabService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除course")
    @ApiOperation("删除course")
    @PreAuthorize("@el.check('zkyCourseTab:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        zkyCourseTabService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}